using AspNetCore.Authentication.Cookies;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddScoped<CookieAuthenticationEvents1>();

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie1("Cookies", options =>
{
    options.EventsType = typeof(CookieAuthenticationEvents1);
})
.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "http://localhost:8080/realms/sso";
    options.RequireHttpsMetadata = false;
    options.MetadataAddress = "http://localhost:8080/realms/sso/.well-known/openid-configuration";
    options.ClientId = "mvc";
    options.ClientSecret = "m1ZA1NGpCGdy6iqmeqwTLF43xWEylG6R";
    options.ResponseType = "code";
    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.Scope.Clear();
    options.Scope.Add("openid");
    options.Scope.Add("profile");
    options.Scope.Add("email");
    options.Scope.Add("offline_access");
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication1();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
